package tank1;

import java.util.Vector;

/**
 * Version: 1.1
 * Author: TangJun
 * Date: 2024-07-30
 */
public class Tank implements Runnable{
    private int x;
    private int y;
    private int direct;//方向
    private int type;//类型
    private int speed = 5;
    private Vector<Bullet> bullets = new Vector<Bullet>();
    private boolean isLive = true;
    private Vector<Tank> enemies;

    //敌方AI坦克
    @Override
    public void run() {
        while (isLive) {
            direct = (int)(Math.random() * 4);
            //发射子弹
            shot();
            //每次改方向后持续移动一段时间
            for (int i = 0; i < 15; i++) {
                switch (direct) {
                    case 0:
                        MoveUp();
                        break;
                    case 1:
                        MoveDown();
                        break;
                    case 2:
                        MoveLeft();
                        break;
                    case 3:
                        MoveRight();
                        break;
                    default:
                        break;
                }
                //每次改方向后休眠
                try {
                    Thread.sleep(80);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }


    public boolean isOverlappedAll(Vector<Tank> enemies){
        for (Tank enemy : enemies) {
            if (this.isOverlapped(enemy)) {
                return true;
            }
        }
        return false;
    }
    //判断是否重叠
    public boolean isOverlapped(Tank tank) {
        boolean ret = false;
        if (tank != null) {
            switch (direct) {
                case 0:
                    ret = isOfRange(x, y, tank)
                            || isOfRange(x+Tank.SIZE+Tank.WIDTH*2, y, tank);
                    break;
                case 1:
                    ret = isOfRange(x, y+Tank.HEIGHT, tank)
                            || isOfRange(x+Tank.SIZE+Tank.WIDTH*2, y+Tank.HEIGHT, tank);
                    break;
                case 2:
                    ret = isOfRange(x, y, tank)
                            || isOfRange(x, y+Tank.HEIGHT, tank);
                    break;
                case 3:
                    ret = isOfRange(x+Tank.SIZE+Tank.WIDTH*2, y, tank)
                            || isOfRange(x+Tank.SIZE+Tank.WIDTH*2, y+Tank.HEIGHT, tank);
                    break;
                default:
                    break;
            }
        }
        return ret;
    }

    //
    public boolean isOfRange(int x, int y, Tank tank) {
        return x-1>=tank.getX() && x+1<=tank.getX()+Tank.HEIGHT
                && y-1>=tank.getY() && y+1<=tank.getY()+Tank.HEIGHT;
    }

/*    @Override
    public String toString() {
        return "x=(" + x + "," + (x+Tank.HEIGHT) + ")"
                + "y=(" + y + "," + (y+Tank.HEIGHT) + ")";
    }*/

    public enum Direction {
        UP, DOWN, LEFT, RIGHT
    }

    public static final int WIDTH = 20;//轮子宽度
    public static final int HEIGHT = 80;//轮子长度
    public static final int SIZE = 40;//炮台大小
    public static final int LENGTH = 50;//炮管长度

    //多颗子弹击中坦克
    public boolean hitAll(Vector<Bullet> bullets) {
        for (Bullet bullet : bullets) {
            boolean ret = hitTank(bullet);
            if (ret) {
                bullets.remove(bullet);
                return true;
            }
        }
        return false;
    }

    //一颗子弹击中坦克
    public boolean hitTank(Bullet bullet) {
        boolean ret = false;
        switch (direct) {
            case 0:
            case 1:
                if (bullet!=null && bullet.getX()+10>x && bullet.getX()<x+Tank.WIDTH*2+Tank.SIZE
                    && bullet.getY()+10>y && bullet.getY()<y+Tank.HEIGHT) {
                    isLive = false;
                    bullet.setLive(false);
                    Recorder.DestroyTank();
                    ret = true;
                }
                break;
            case 2:
            case 3:
                if (bullet!=null && bullet.getX()+10>x && bullet.getX()<x+Tank.HEIGHT
                        && bullet.getY()+10>y && bullet.getY()<y+Tank.WIDTH*2+Tank.SIZE) {
                    isLive = false;
                    bullet.setLive(false);
                    Recorder.DestroyTank();
                    ret = true;
                }
                break;
            default:
                break;
        }
        return ret;
    }
    //开始发射子弹
    public void shot() {
        Bullet bullet = null;
        switch (direct) {
            //向上
            case 0:
                bullet = new Bullet(x+ Tank.WIDTH + Tank.SIZE/2, y + Tank.HEIGHT/2 - Tank.LENGTH, direct);
                break;
            //向下
            case 1:
                bullet = new Bullet(x+ Tank.WIDTH + Tank.SIZE/2, y + Tank.HEIGHT/2 + Tank.LENGTH, direct);
                break;
            //向左
            case 2:
                bullet = new Bullet(x+ Tank.WIDTH + Tank.SIZE/2 - Tank.LENGTH, y + Tank.WIDTH+Tank.SIZE/2, direct);
                break;
            //向右
            case 3:
                bullet = new Bullet(x+ Tank.WIDTH + Tank.SIZE/2 + Tank.LENGTH, y + Tank.WIDTH+Tank.SIZE/2, direct);
                break;
            default:
                break;
        }
        bullet.setColor(type);
        bullets.add(bullet);
        new Thread(bullet).start();
    }
    public Tank(int x, int y, int type) {
        this.x = x;
        this.y = y;
        this.type = type;
    }

    //上移
    public void MoveUp(){
        direct = 0;
        if (y > 0 && !isOverlappedAll(enemies)) {
            y -= speed;
        }
    }
    //下移
    public void MoveDown(){
        direct = 1;
        if (y + Tank.HEIGHT + 60 < TankFrame.HEIGHT && !isOverlappedAll(enemies)) {
            y += speed;
        }
    }
    //左移
    public void MoveLeft(){
        direct = 2;
        if (x > 0 && !isOverlappedAll(enemies)) {
            x -= speed;
        }
    }
    //右移
    public void MoveRight(){
        direct = 3;
        if (x + Tank.HEIGHT + 20 < TankFrame.WIDTH && !isOverlappedAll(enemies)) {
            x += speed;
        }
    }
    public void set(int x, int y, int direct) {
        this.x = x;
        this.y = y;
        this.direct = direct;
    }

    public void setEnemies(Vector<Tank> enemies) {
        this.enemies = enemies;
    }

    public boolean isLive() {
        return isLive;
    }

    public void setLive(boolean live) {
        isLive = live;
    }

    public Vector<Bullet> getBullets() {
        return bullets;
    }
    public int getSpeed() {
        return speed;
    }

    public void setSpeed(int speed) {
        this.speed = speed;
    }
    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }


    public int getDirect() {
        return direct;
    }

    public void setDirect(int direct) {
        this.direct = direct;
    }


    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    public void setX(int x) {
        this.x = x;
    }
}
